<template>
  <base-breadcrumb :breadcrumb="!isAudit">
    <a-card :bordered="false" style="over-flow:scroll">
      <title-name title="查看设计策划书"></title-name>
      <a-form-model :model="form" ref="ruleForm" :rules="rules"
                        :labelCol="{lg: {span: 6}, sm: {span:6}}"
                        :wrapperCol="{lg: {span: 18}, sm: {span: 18} }">
        <a-row>
          <a-col :md="8" :sm="8">
            <a-form-model-item label="分公司"
                          :labelCol="{lg: {span: 6}, sm: {span:6}}"
                          :wrapperCol="{lg: {span: 18}, sm: {span: 18} }" prop="branchId">
              <!-- <SelectSubCompany placeholder="请选择" v-model="form.branchId" @change="handleChangeCompany" :disabled="type=='view'"></SelectSubCompany> -->

               <BaseInput v-model="form.branchCompany" disabled />
            </a-form-model-item>
          </a-col>
        <a-col :md="8" :sm="8">
          <a-form-model-item label="项目名称" prop="projectId">
            <BaseInput v-model="form.project" disabled />
          </a-form-model-item>
        </a-col>
          <a-col :md="8" :sm="8">
            <a-form-model-item
                    label="项目设计管理类别" prop="designType">
              <BaseInput v-model="form.designType" disabled   />
            </a-form-model-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-model-item
                    label="设计策划书名称" prop="name">
              <BaseInput v-model="form.name" disabled   />
            </a-form-model-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-model-item
                    label="双优化立项计划表" prop="optimization">
              <BaseInput v-model="form.optimization" disabled  />
            </a-form-model-item>
          </a-col><a-button @click="openToTable" type="primary" style="margin-left:20px">查看</a-button>
        </a-row>
        <a-row>
          <a-col :md="8" :sm="8">
            <a-form-model-item
                    label="创建人" prop="person">
              <BaseInput v-model="form.person" disabled   />
            </a-form-model-item>
          </a-col>
          <a-col :md="8" :sm="8">
            <a-form-model-item
                    label="创建时间" prop="user">
              <BaseInput v-model="form.time" disabled  placeholder="请输入" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
      <title-name  title="设计策划书/盈亏分析表"></title-name>
        <a-table bordered :columns="columnAssment" :data-source="dataAssement" :rowSelection="rowSelection">
          <span slot="serial"  slot-scope="text, record, index">{{ index + 1 }}</span><span slot="action"  slot-scope="text, record, index">
            <a :href="'/api/ycloud-file-center/oper/download?access_token='+token+'&fileId='+record.fileId" target="_parent">导出</a>
                    <a-divider type="vertical" />
          <a
                  href="javascript:"
                  @click="$filePreview(record)"
          >预览</a>
        </span>
        </a-table>


    </a-card>
    <a-modal
            title="查看双优化立项计划表"
            :visible="visible"
            :confirm-loading="confirmLoading"
            width="1000px"
            @ok="handleOk"
            @cancel="handleCancel">
      <a-form>
      <title-name title="双优化立项计划基本信息"></title-name>
        <a-row>
          <a-col :md="12" :sm="12">
            <a-form-model-item
                    label="双优化立项计划表"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.name" disabled    />
            </a-form-model-item>
          </a-col>

          <a-col
                  :md="12" :sm="12">
            <a-form-model-item
                    label="分公司"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.branchOffice" disabled   itemtype=""/>
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-model-item
                    label="项目"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.project" disabled   />
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-model-item
                    label="创建人"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.filledBy" disabled  />
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-model-item
                    label="编制时间"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.createTime" disabled   />
            </a-form-model-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <a-form-model-item
                    label="状态"
                    :labelCol="{ lg: { span: 6 }, sm: { span: 6 } }"
                    :wrapperCol="{ lg: { span: 18 }, sm: { span: 18 } }">
              <BaseInput v-model="formAdd.chiStatus" disabled   />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form>

      <title-name title="计划项信息"></title-name>
      <a-table
      :columns="columns"
      :data-source="tableData"
      >
      <span slot="serial" slot-scope="text, record, index">{{ index + 1 }}</span>
      </a-table>

    </a-modal>
    <div v-if="!isConsultation" class="footer page-btn-right-top">
      <a-button type="primary"  @click="goBack">关闭</a-button>
    </div>
  </base-breadcrumb>
</template>

<script>
  import Vue from 'vue'
  import { STable, Ellipsis } from '@/components'
  import { getProjectType } from '@/api/design/plotsPlan'
  import { planList} from '@/api/technical/optimizationPlan'
  import { getplotsManage , getplotsManageTotal,getplotsManageEdit  ,getplotsManageDetail,deleteFiles ,getplotsManageAdd} from '@/api/design/designManage'
  // import { themeChoose } from '@/mixin/mixin'
  import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
  import SelectProject from '@/components/Select/SelectProject' // 项目
  import { uploadFile,deleteUploadFile } from '@/api'
  import { getAuditProject ,getCurrentUserBranchCom} from '@/api/themeChoose/themeChoose'

  const columns = [
    {
      title: '序号',
      dataIndex: 'serial',
      scopedSlots: { customRender: 'serial' }
    },
    {
      title: '双优化名称',
      dataIndex: 'name'
    },
    {
      title: '双优化分类',
      dataIndex: 'categoryName',
    },
    {
      title: '详细类型',
      dataIndex: 'detailedCategoryName',
    },
    {
      title: '内容描述',
      dataIndex: 'content',
    },
    {
      title: '技术实施时间',
      dataIndex: 'startTime',
    },
    {
      title: '提出人',
      dataIndex: 'proposer',
    },
    {
      title: '拟实施人',
      dataIndex: 'implement',
    },
    {
      title: '预测效益(万)',
      dataIndex: 'benefit',
    }
  ]
  const columnAssment = [
    {
      title: '序号',
      dataIndex: 'serial',
      width: '5%',
      scopedSlots: { customRender: 'serial' },
    },
    {
      title: '附件名称',
      dataIndex: 'fileName',
      width: '35%',
    },
    {
      title: '附件格式',
      dataIndex: 'suffixName',
      width: '35%',
    },
    {
      title: '操作',
      key: 'action',
      width: '20%',
      scopedSlots: { customRender: 'action' },
    },
  ];
  export default {
    name:'a'+ Date.now(),
    components:{
      STable,
      SelectSubCompany,
      SelectProject
    },
    props: {
      // 是否作为审批详情
      isAudit: {
        type: Boolean,
        default: false
      },
      // 是否作为意见征询
      isConsultation: {
        type: Boolean,
        default: false
      }
    },
    data(){
      // this.searchArr = searchArr
      this.columns = columns
      return{
        form:{
          time:this.getNowTime()
        },
        // 查询参数
        queryParam: {},
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          const requestParameters = Object.assign({}, parameter, this.queryParam)
          return planList(requestParameters)
            .then(res => {
              return res
            })
        },
        selectedRowKeys:'',
        loading:false,
        selectedRows:'',
        fileArrysDataList:[],
        type:'',
        formAdd:{
          time:this.getNowTime(),
          person:''
        },
        tableData:[],
        confirmLoading:false,
        disabled:false,
        id:'',
        columnAssment,
        dataAssement:[],
        uploadFile:{},
        deleteObj:{},
        token:'',
        fileFileds:null,
        branchCompanyLists:[],
        fileIds:[],
        // 附件上传
        listUrl:'',
        deleteUrl:'/cscec-project-business/file/business/project-fileDelete',
        uploadList:'',
        businessId:'',
        businessCode:'PM25',
        uploadArrys:[],
        designType:'',
        reqMethod:'post',
        projectLists:[],
        name:'',
        status:'',
        pageType:'',
        jobsList:[],
        visible:false,
        user:'',
        rules: {
            // optimization: [{ required: true, message: '请关联双优化立项计划表', trigger: 'change' }],
            branchId: [{ required: true, message: '请选择单位名称', trigger: 'change' }],
            projectId: [{ required: true, message: '请选择项目', trigger: 'change' }],
        },
      }
    },
  computed: {
    rowSelection() {
      return {
        onChange: (selectedRowKeys, selectedRows) => {
          console.log(`selectedRowKeys: ${selectedRowKeys}`, 'selectedRows: ', selectedRows);
          this.selectedRowKeys = selectedRowKeys
          this.selectedRows = selectedRows
          let fileFileds = []
          let fileIds = []
          selectedRows.forEach(function(obj,i) {
            fileFileds.push(obj.fileId)
            console.log(fileIds)
            fileIds.push(obj.commonId)
          })
          this.fileFileds = fileFileds.join(',')   // 删除公共附件
          this.fileIds = fileIds                   // 有业务id 删除附件
          },
      };
    },
  },
    created() {

      console.log(this.formAdd.user)
      this.type=this.$route.query.type
      this.id = this.$route.query.id.replace("_knowledge", '')
      if(!this.id) {
        // 获取登录人名称
        this.form.person = JSON.parse(sessionStorage.getItem('ycloud-user_info')).employee.name
      }
      this.token = Vue.getAcloudProvider().getAccessToken()
      this.getCurrentUserBranchCom()
      this.getplotsManageDetail()
    },
    methods:{
      openToTable(){
        this.planList()
      },
      getCurrentUserBranchCom(){
        this.loading=true
        getCurrentUserBranchCom({ tagCodes: ['BC'] }).then(res=>{
          this.branchCompanyLists = res.data
          console.log(this.branchCompanyLists)
          if(this.type=='add'){
            console.log(this.branchCompanyLists)
            this.$set(this.form, 'branchId', this.branchCompanyLists[0].orgId)
            console.log(this.form.branchId)
            this.handleChangeCompany()
          }
          this.loading=false
        })
      },
      getplotsManageDetail(){
        if(this.id){
          getplotsManageDetail({id:this.id}).then(res=>{
            this.form = res.data
            this.name = res.data.name
            this.form.branchId = res.data.branchCompanyId
            this.dataAssement = res.data.fileIdsInfo || []
            console.log(this.fileArrysData)
            getAuditProject({branchComIds:[this.form.branchId],ifDesignPlanBook:0,auditStatus:2}).then(res=>{
              this.projectLists = res.data
              let arr = {}
              arr={
                projectId:this.form.projectId,
                designTypeId:this.form.designTypeId,
                designTypeName:this.form.designType,
                projectName:this.form.project,
              }
              this.projectLists.push(arr)
            })
          })
        }
      },
      planList(){
        if(this.form.projectId){
          planList({pageSize:10,pageNo:1,query:{project:this.form.projectId}}).then(res=>{
              this.visible=true
              this.tableData = res.data[0].doubleOptimizationTypeVO
              this.formAdd = res.data[0]
          })
        }else{
          this.$message.error("请先选择要关联的项目")
        }
      },
      fileArrysData(val){
        this.fileArrysDataList = val
      },

      onSelectChanges(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
        let fileFileds = []
        let fileIds = []
        selectedRows.forEach(function(obj,i) {
          fileFileds.push(obj.fileId)
          fileIds.push(obj.commonId)
        })
        console.log(fileIds)
        this.fileFileds = fileFileds.join(',')   // 删除公共附件
        this.fileIds = fileIds                   // 有业务id 删除附件
      },
      getPopupContainer(triggerNode){
        return  triggerNode.parentNode
      },
      deleteExteriorFile(){
        let that = this;
        if(that.selectedRowKeys.length == 0){
          that.$message.error("请先选中需要删除的附件！")
          return;
        }
        if(that.id){
          that.$confirm({
            title: '删除',
            content: '确定删除附件吗？',
            onOk() {
              deleteFiles({businessId:that.id,fileList:that.fileIds}).then(res=>{
                if(res.code == 200 || res.data.code == 200){
                  that.$message.success("删除附件成功！")
                  that.getDetails()
                }
              })
            },
            onCancel() {},
          });
        }else{
          that.$confirm({
            title: '删除',
            content: '确定删除附件吗？',
            onOk() {
              deleteUploadFile({fileIds:that.fileFileds}).then(res=>{
                if(res.code == '0' || res.data.code == '0'){
                  let data = that.dataAssement
                  let selectDatas = that.fileIds
                  console.log(selectDatas)
                  that.dataAssement =  data.filter(item => { return !selectDatas.includes(item.commonId); });
                  console.log("hfdkjsaf:",that.dataAssement[0].id)
                }
              })
            },
            onCancel() {},
          });

        }

      },
      // 上传

      beforeUpload(info){
        this.uploadFile = info
        return false
      },

      handleChanges(info) {
        const data = new FormData()
        data.append("file",this.uploadFile)
        data.append("businessCode",this.businessCode)

        uploadFile( data ).then(res => {
          if(res.code === 200 ){
            // let  obj = {}
            // obj.fileName = res.data.fileName
            // obj.fileType =  res.data.fileType
            // obj.url =  res.data.url
            this.dataAssement.push(res.data)
            this.form.uploadFileList = this.dataAssement
          }
        })
      },
      goBack(){
        window.close()
      },
      handleChangeCompany(value){
        console.log(this.form)
        if(this.form.branchId){
          getAuditProject({branchComIds:[this.form.branchId],ifDesignPlanBook:0,auditStatus:2}).then(res=>{
            this.projectLists = res.data
            console.log(this.projectLists)
            this.$set(this.form, 'projectId', this.projectLists[0].projectId)
        })
        }
      },
      changeProjectType(value){
        this.form.projectId = value
        console.log(value)
        let arr=[]
        arr.push(value)
        getProjectType({projectIds:arr}).then(res=>{
          console.log(res.data[0])
          this.form.num = res.data[0].projectNumber
          this.form.designType = res.data[0].designTypeName
          this.form.designTypeId = res.data[0].designTypeId
          this.name = res.data[0].name+'设计策划书'
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      search(values){
        this.queryParam = values
        this.$refs.table.refresh(true)
      },
      goAdd(){
        // this.visible = true
      },
      handleSub(record,type){
        this.visible = true
        this.type = type
        this.form = record
      },
      handleEdit(record){
        this.visible = true
        this.form = record
      },
      handleOk(){
        this.visible = false
        this.form.optimization = this.formAdd.name
        this.form.optimizationId = this.formAdd.id
      },
      handleCancel(){
        this.visible = false
      }
    }
  }
</script>

<style scoped>
  .input{
    width:100%
  }
  .footer{
    float:right;
    margin-right:10px
  }
  .footer button{
    margin-left:10px
  }
</style>
